package com.lei.chinese_herbal_medicine.Controller;


import com.lei.chinese_herbal_medicine.Entity.Herb;
import com.lei.chinese_herbal_medicine.Entity.PageData;
import com.lei.chinese_herbal_medicine.Entity.Result;
import com.lei.chinese_herbal_medicine.Entity.TrainDataset;
import com.lei.chinese_herbal_medicine.Service.HerbService;
import com.lei.chinese_herbal_medicine.Service.TrainDatasetService;
import com.lei.chinese_herbal_medicine.Util.UUIDUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (TrainDataset)表控制层
 *
 * @author makejava
 * @since 2024-01-18 10:05:28
 */
@RestController
public class TrainDatasetController {
    /**
     * 服务对象
     */
    @Resource
    private TrainDatasetService trainDatasetService;
    @Resource
    private HerbService herbService;
    @Resource
    Result result;

    @DeleteMapping("/deleteTrainDataset")
    public Result deleteTrainDataset(@RequestBody TrainDataset trainDataset){
        Map map=new HashMap();
        if(trainDatasetService.deleteByPrimaryKey(trainDataset.getId())){
            result.setCode(200);
            map.put("message","删除成功");
        }else{
            result.setCode(403);
            map.put("message","删除失败");
        }
            result.setData(map);
        return result;
    }

    @GetMapping("/getTrainDataset")
    public Result getTrainDataset(Herb herb, PageData pageData){
        List<TrainDataset> list=trainDatasetService.selectTrainDatasetByHerbId(herb.getId(),pageData);
        result.setCode(200);
        Map map=new HashMap();
        map.put("pageData",pageData);
        map.put("message","查询成功");
        result.setList(list);
        result.setData(map);
        return result;
    }

    @PostMapping("/addTrainDataset")
    public Result addTrainDataset(MultipartFile file, String id){
        TrainDataset trainDataset=new TrainDataset();
        trainDataset.setId(UUIDUtil.fastSimpleUUID());
        trainDataset.setHerbId(id);
        trainDataset.setTrainDatasetName(herbService.selectByPrimaryKey(id).getLatinName());
        boolean flag=true;
        try {
            trainDataset.setImg(file.getBytes());
            flag=trainDatasetService.insert(trainDataset);
        } catch (IOException e) {
            flag=false;
            System.out.println(e.getMessage());
        }
        Map map=new HashMap();
        if(flag){
            result.setCode(200);
            map.put("message","训练集图片添加成功");
        }else{
            result.setCode(403);
            map.put("message","图片太大或其他原因，添加失败");
        }
        result.setData(map);
        return result;
    }
}

